// ============================================================================
//
// Copyright (C) 2009-2011 SC Technology Group. - www.sc.com
//
// ============================================================================
package com.sc.vessel.info.editor;

import java.util.Iterator;

import org.eclipse.core.commands.HandlerEvent;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.emf.common.util.EList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.ibm.team.jface.ConfigComposite;
import com.ibm.team.ui.editor.TeamFormPart;
import com.sc.vessel.info.model.breakdown.ConfigElement;
import com.sc.vessel.info.model.breakdown.ConfigElementSelected;

/**
 * DOC liaoqc class global comment. Detailled comment
 */
public class ConfigPart extends TeamFormPart {

    private InfoInput infoInput;

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.team.ui.editor.TeamFormPart#setInput(java.lang.Object)
     */
    @Override
    public void setInput(Object input) {
        infoInput = (InfoInput) input;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.team.ui.editor.TeamFormPart#createContent(org.eclipse.swt.widgets.Composite)
     */
    @Override
    public void createContent(Composite parent) {
        // parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
        parent.setLayout(new GridLayout());
        EList<ConfigElementSelected> configElementSelecteds = infoInput.getAccident().getConfigElements();
        for (Iterator<ConfigElementSelected> iterator = configElementSelecteds.iterator(); iterator.hasNext();) {
            final ConfigElementSelected configElementSelected = (ConfigElementSelected) iterator.next();
            ConfigElement configElement = configElementSelected.getConfigElement();
            ConfigComposite configComposite = new ConfigComposite(parent, SWT.NONE, configElement, configElementSelected) {

                /*
                 * (non-Javadoc)
                 * 
                 * @see com.ibm.team.jface.ConfigComposite#setSelectedValue(java.lang.String)
                 */
                @Override
                protected void setSelectedValue(String text) {
                    super.setSelectedValue(text);
                    ConfigPart.this.notifyListeners(configElementSelected, text);

                }

            };
            // configComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
            configComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
            // configComposite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
        }
    }

    private void notifyListeners(ConfigElementSelected configElementSelected, String text) {
        HandlerEvent handlerEvent = new HandlerEvent(this, true, false);
        Object[] listeners = getListeners();
        for (int i = 0; i < listeners.length; i++) {
            Object listener = listeners[i];
            if (listener instanceof IHandlerListener) {
                IHandlerListener handlerListener = (IHandlerListener) listener;
                handlerListener.handlerChanged(handlerEvent);
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.commands.AbstractHandler#fireHandlerChanged(org.eclipse.core.commands.HandlerEvent)
     */
    @Override
    protected void fireHandlerChanged(HandlerEvent handlerEvent) {
        // TODO Auto-generated method stub
        // super.fireHandlerChanged(handlerEvent);
    }

}
